package controlLayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import modelLayer.*;

public class SaleController {
    ItemController itemController = new ItemController();
    
    public boolean cancelSale(Map<Integer,Integer> line){
    	System.out.println("Cancelling sale");
    	 Iterator itr = line.keySet().iterator();
    	 while(itr.hasNext()){ 
             int barcode = (int)itr.next();
             int quantity = line.get(barcode);
             System.out.println(barcode+"/"+quantity);
             itemController.findItem(barcode).setQuantity(itemController.findItem(barcode).getQuantity()+quantity);
         }
    	 for(Item i: itemController.getAllItems()){
    		System.out.println("Name: "+i.getName()+" Quantity: "+i.getQuantity()) ;
    	 }
    	 
    	System.out.println("Sale canceled. Items were restored");
    return false;
    }
    
    public Sale makeSale(String username, int cprNo, Map<Integer,Integer> line){
        Date date = new Date();
        Sale sale = new Sale(0,date.toString(),addStaff(username));//not done yet
        double price=sale.getTotalPrice();
        //go through the keys of the map 
        Iterator itr = line.keySet().iterator();
        while(itr.hasNext()){ // while it still has keys to search for 
            int barcode = (int)itr.next();
            int quantity = line.get(barcode);
            //make a saleLine with the item and it's quantity
            SaleLine saleLine = addItemTo(barcode,quantity);
            //if created successfully, add it inside the sale;
            if(saleLine != null){
                sale.addSaleLine(saleLine);
                price = sale.getTotalPrice() + saleLine.getPrice();
                sale.setTotalPrice(price);
            }                
        }
        

        
        //apply the discount to the sale
        price = sale.getTotalPrice() * (100-checkDiscount(sale, cprNo)) / 100;
        sale.setTotalPrice(price);
        return sale;
    }

    public SaleLine addItemTo(int barcode, int quantity){
        ItemController itemCTR = new ItemController();
        Item item = itemCTR.findItem(barcode);
        double price = itemCTR.findItem(barcode).getPrice();
        /*
         * Check if the quantity is not bigger than the quantity of the item
         */
       // if(itemCTR.checkQuantity(barcode, quantity)){
            //update the quantity inside the modelLayer
         //   item.setQuantity((item.getQuantity()-quantity));
            price = price*quantity; // set the price for SaleLine
            //create the saleLine with the parameters
            SaleLine saleLine = new SaleLine(barcode, price, quantity);
            return saleLine;
       // }
    }
    
    public Staff addStaff(String username){
        StaffController staffCTR = new StaffController();
        return staffCTR.findStaffByUserName(username);
    }
    
    public Customer addCustomer(int cprNo){
        CustomerController customerCTR = new CustomerController();
        return customerCTR.findCustomer(cprNo);
    }
    
    public Item findItemByBarcode(int barcode){
        ItemController itemCTR = new ItemController();
        return itemCTR.findItem(barcode);
    }
    
    public double checkDiscount(Sale sale, int cprNo){
        double discount = 0;
        int totalQuantity = 0;
        ArrayList<SaleLine> saleLines = sale.getSaleLines();
        double totalPrice = sale.getTotalPrice();
        CustomerController customerCTR = new CustomerController();
        
        //get the total quantity
        for(SaleLine sl : saleLines){
            totalQuantity = sl.getQuantity();
        }
        Discount d = new QuantityDiscount();
        //add Quantity Discount;
        if(totalQuantity >= ((QuantityDiscount)d).getMinQuantity()){
            discount += d.getAmountInPercent();
        }
        //get the person Discount
        if(cprNo != 0){
            discount += customerCTR.getDiscount(cprNo);
            Customer c = customerCTR.findCustomer(cprNo);
            //add CraftsMan Discount;
            if(c.getIsCraftsman()){
                discount += d.getAmountInPercent();
            }
        }
        if(discount > 20){
            discount = 20;
        }
        return discount;
    }
    
    public String makePayment(Sale sale, double money){
        double totalPrice2 = sale.getTotalPrice();
        double change = 0 ;
        change = money - totalPrice2;
        if(change >= 0)
            return endSale(sale);
        else
        return "Not Done Yet! More Money!";
    }
    
    public String endSale(Sale sale){
        SaleContainer saleContainer = SaleContainer.getInstance();
        saleContainer.addSale(sale);
        return "Successfully finished the sale!";
    }
}
